package com.zzuli.controller;

import com.zzuli.entity.*;
import com.zzuli.service.RepairInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/repair")
public class RepairInfoController {
    @Autowired
    private RepairInfoService repairInfoService;

    @GetMapping("/selectByPage")
    public Result selectByPage(Integer page, Integer limit, RepairInfoEntity repairInfoEntity) {
        Map<String, Object> data = repairInfoService.selectAll(page, limit, repairInfoEntity);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(data);
        return result;
    }

    @PostMapping("/addRepair")
    public Result addRepair(HttpServletRequest request) {
//        Result result = new Result();
//        boolean flag = repairInfoService.addRepair(repairInfoEntity);
//        if (flag) {
//            result.setCode(200);
//            result.setMsg("设备保修信息添加成功");
//        } else {
//            result.setCode(500);
//            result.setMsg("设备保修信息添加失败");
//        }
//        return result;
        Result result = new Result();
        AreaInfoEntity areaInfoEntity = new AreaInfoEntity();
        DeviceInfoEntity deviceInfoEntity = new DeviceInfoEntity();
        RepairInfoEntity repairInfoEntity = new RepairInfoEntity();
        UserInfoEntity userInfoEntity = new UserInfoEntity();

        deviceInfoEntity.setDeviceName(request.getParameter("repairName"));
        deviceInfoEntity.setDeviceType(request.getParameter("repairType"));

        areaInfoEntity.setBuild(request.getParameter("build"));
        areaInfoEntity.setFloor(request.getParameter("floor"));

        repairInfoEntity.setRemark(request.getParameter("remark"));
        repairInfoEntity.setSpLocation(request.getParameter("spLocation"));


        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("areaInfoEntity", areaInfoEntity);
        //总数据条目
        map.put("deviceInfoEntity", deviceInfoEntity);

        map.put("repairInfoEntity", repairInfoEntity);

        map.put("userInfoEntity", userInfoEntity);

        boolean flag = repairInfoService.addRepair(map);
        if(flag){
            result.setCode(200);
            result.setMsg("设备保修信息添加成功");
        }else {
            result.setCode(500);
            result.setMsg("设备保修信息添加失败");
        }
        return result;
    }

    @PostMapping("/updateRepair")
    public Result updateUser(RepairInfoEntity repairInfoEntity) {
//        Result result = new Result();
//        boolean flag = repairInfoService.updateRepair(repairInfoEntity);
//        if (flag) {
//            result.setCode(200);
//            result.setMsg("设备保修信息修改成功");
//        } else {
//            result.setCode(500);
//            result.setMsg("设备保修信息修改失败");
//        }
//        return result;
        return null;
    }

    @PostMapping("/deleteRepair")
    public Result deleteUser(HttpServletRequest request) {
        Result result = new Result();
        String[] RepairIds = request.getParameterValues("allRepairId[]");
        boolean flag = repairInfoService.deleteRepair(RepairIds);
        if (flag){
            result.setCode(200);
            result.setMsg("设备保修信息删除成功");
        }else {
            result.setMsg("设备保修信息删除失败");
        }
        return result;
    }

    @PostMapping("/updateRepairStatus")
    public Result updateRepairStatus(RepairInfoEntity repairInfoEntity){
        Result result = new Result();
        boolean flag = repairInfoService.updateRepairStatus(repairInfoEntity);
        if (flag){
            result.setCode(200);
            result.setMsg("设备报修状态修改成功");
        }else {
            result.setMsg("设备报修状态修改失败");
        }
        return result;
    }

    @GetMapping("/selectStatusCount")
    public Result selectStatusCount(){
        Result result = new Result();
        List<Integer> list = repairInfoService.selectStatusCount();
        if (list != null){
            result.setCode(200);
            result.setMsg("报修状态查询成功");
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("报修状态查询失败");
        }
        return result;
    }

    @GetMapping("/accept")
    public Result accept(RepairInfoEntity repairInfoEntity){
        Result result = new Result();
        boolean flag = repairInfoService.accept(repairInfoEntity);
        if (flag){
            result.setCode(200);
            result.setMsg("受理成功");
        }else {
            result.setCode(500);
            result.setMsg("受理失败");
        }
        return result;
    }
}
